package com.hongye.broker.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongye.broker.mapper.RentHouseMapper;
import com.hongye.broker.service.RentHouseService;
import com.hongye.common.entity.RentHouseEntity;
import org.springframework.stereotype.Service;
import java.util.List;
import cn.hutool.core.util.ObjectUtil;
import com.hongye.broker.mapper.HouseTypeMapper;
import com.hongye.broker.vo.HouseSelectVo;
import com.hongye.common.response.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap;

@Service
public class RentHouseServiceImpl extends ServiceImpl<RentHouseMapper, RentHouseEntity> implements RentHouseService {


    @Override
    public IPage<RentHouseEntity> zu(Integer page,Integer size,String rentHouseTitle) {
        QueryWrapper<RentHouseEntity> query = Wrappers.query();
        query.eq("status", 0);
        query.orderByDesc("update_time");
        query.like("rent_house_title", rentHouseTitle);
        IPage<RentHouseEntity> p = new Page<>(page, size);
        return this.page(p, query);
    }

    @Override
    public void updateHouse(RentHouseEntity rentHouseEntity) {
        baseMapper.updateById(rentHouseEntity);
    }

    @Override
    public List<RentHouseEntity> rentList() {
        QueryWrapper<RentHouseEntity> query = new QueryWrapper<>();
        query.eq("es_status","0");
        return baseMapper.selectList(query);
    }

    @Override
    public ResponseResult list(HouseSelectVo houseSelectVo) {
        System.out.println(ObjectUtil.isNull(houseSelectVo));
        if (ObjectUtil.isNull(houseSelectVo)){
            QueryWrapper<RentHouseEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("create_time");
            List<RentHouseEntity> list = baseMapper.selectList(queryWrapper);
            HashMap<String, Object> map = new HashMap<>();
            map.put("list",list);
            return ResponseResult.ok(1,"ok",map);
        }else {
            QueryWrapper<RentHouseEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("country_id",houseSelectVo.getCountry());
            queryWrapper.between("price",houseSelectVo.getMinPrice(),houseSelectVo.getMaxPrice());
            queryWrapper.eq("room_type",houseSelectVo.getRoomType());
            queryWrapper.eq("orientation",houseSelectVo.getOrientation());
            queryWrapper.orderByDesc("create_time");

            List<RentHouseEntity> houseEntities = baseMapper.selectList(queryWrapper);
            HashMap<String, Object> map = new HashMap<>();
            map.put("list",houseEntities);
            return ResponseResult.ok(1,"ok",map);
        }
    }
}
